ohibc logo
OHI British Columbia | OHI Science | Citation policy

knitr::opts_chunk$set(fig.width = 6, fig.height = 4, fig.path = 'Figs/',
                      echo = TRUE, message = FALSE, warning = FALSE)

library(ohicore) ### devtools::install_github('ohi-science/ohicore')

source('~/github/ohibc/src/R/common.R')

dir_ohibc  <- '~/github/ohibc'
dir_calc   <- file.path(dir_ohibc, 'calc_ohibc')
dir_master <- file.path(dir_calc, 'master')

source(file.path(dir_calc, 'calc_scores_fxns.R'))

### provenance tracking
# library(provRmd); prov_setup()

1 Index score plots

scores <- read_csv(file.path(dir_calc, 'scores_all.csv')) %>%
  filter(dimension %in% c('score')) %>%
  left_join(get_rgn_names(), by = c('region_id' = 'rgn_id'))

scores_tmp <- scores %>%
  filter(goal == 'Index')

status_plot <- ggplot(scores_tmp %>% filter(region_id != 0), 
                    aes(x = year, y = score, color = rgn_name)) +
  ggtheme_plot() +
  geom_line(data = scores_tmp %>% 
              filter(region_id == 0), 
            aes(x = year, y = score), size = 1.5, color = 'grey20', alpha = .8) +
  geom_line(aes(group = region_id), alpha = .4, size = 1) +
  scale_x_continuous(breaks = scores_tmp$year %>% unique() %>% sort) +
  scale_y_continuous(limits = c(0, 100)) +
  theme(axis.text.x = element_text(angle = 30)) +
  labs(color = 'Index') +
  guides(colour = guide_legend(override.aes = list(size = 3)))

print(status_plot)

scores_sum <- scores_tmp %>%
  select(region_id, rgn_name, score, year) %>%
  group_by(region_id, rgn_name) %>%
  mutate(indx_mean = round(mean(score), 3), 
         indx_sd   = round(sd(score), 3)) %>%
  filter(score == min(score) | score == max(score)) %>%
  mutate(score = round(score, 1))

DT::datatable(scores_sum)

2 Status plots

scores <- read_csv(file.path(dir_calc, 'scores_all.csv')) %>%
  filter(dimension %in% c('status')) %>%
  spread(dimension, score) %>%
  filter(!is.na(status)) %>%
  left_join(get_rgn_names(), by = c('region_id' = 'rgn_id'))

goals <- c('AO' = 'First Nations Resource Access Opportunity',
           'HAB' = 'BD: Habitats', 'SPP' = 'BD: Species', 'BD' = 'Biodiversity', 
           'CPP' = 'ES: Coastal Protection', 'CSS' = 'ES: Carbon Storage', 'ES' = 'Ecosystem Services', 
           'CW' = 'Clean Waters',  
           'FIS' = 'FP: Wild-capture Fisheries', 'MAR' = 'FP: Mariculture', 
           'SAL' = 'FP: Salmon', 'FP' = 'Food Provision',
           'ICO' = 'SP: Iconic Species', 'LSP' = 'SP: Lasting Special Places', 'SP' = 'Sense of Place', 
           'LEF' = 'LE: First Nations Livelihoods', 'LEO' = 'LE: Non-First Nations Livelihoods', 'LE' = 'Livelihoods', 
           'TR' = 'Tourism and Recreation')

for(i in seq_along(goals)) {
  
  goal_code = names(goals)[i]
  goal_name = goals[i]
  
  scores_tmp <- scores %>%
    filter(goal == goal_code)
  status_plot <- ggplot(scores_tmp %>% 
                          filter(region_id != 0), 
                      aes(x = year, y = status, color = rgn_name)) +
    ggtheme_plot() +
    geom_line(data = scores_tmp %>% 
                filter(region_id == 0), 
              aes(x = year, y = status), size = 1.5, color = 'grey20', alpha = .8) +
    geom_line(aes(group = region_id), alpha = .7, size = 1) +
    scale_x_continuous(breaks = scores_tmp$year %>% unique() %>% sort) +
    scale_y_continuous(limits = c(0, 100)) +
    theme(axis.text.x = element_text(angle = 30)) +
    labs(color = goal_code,
         title = goal_name) +
    guides(colour = guide_legend(override.aes = list(size = 3)))

  print(status_plot)
}

3 Trend plots

scores <- read_csv(file.path(dir_calc, 'scores_all.csv')) %>%
  filter(dimension %in% c('trend')) %>%
  left_join(get_rgn_names(), by = c('region_id' = 'rgn_id'))

for(goalname in scores$goal %>% unique() %>% sort) {
  # goalname <- scores$goal[1]
  scores_tmp <- scores %>%
    filter(goal == goalname)
  trend_plot <- ggplot(scores_tmp %>% 
                          filter(region_id != 0), 
                      aes(x = year, y = score, color = rgn_name)) +
    ggtheme_plot() +
    geom_line(aes(group = region_id), alpha = .7) +
    scale_x_continuous(breaks = scores_tmp$year %>% unique() %>% sort) +
    scale_y_continuous(limits = c(-1, 1)) +
    theme(axis.text.x = element_text(angle = 30)) +
    labs(color = goalname) +
    guides(colour = guide_legend(override.aes = list(size = 3)))

  print(trend_plot)
}

4 Data layer year spans

Clipped to 1990 and later; some data layers go back farther but these will not typically inform scores except as reference points.

layer_targets <- read_csv(file.path(dir_calc, 'explore/int/layers_targets_master.csv')) %>%
  select(-target_element, -dimension) %>%
  distinct()

data_years <- read_csv(file.path(dir_calc, 'master/all_data_years.csv'))

# no_year_spans <- layer_targets %>% 
#   filter(!layer %in% data_years$layer_name) %>%
#   group_by(layer) %>%
#   summarize(targets = paste(target, collapse = ', '))
# 
# knitr::kable(no_year_spans) %>% paste(collapse = '')

year_spans <- data_years %>%
  full_join(layer_targets, by = c('layer_name' = 'layer')) %>%
  filter(target != 'spatial') %>%
  group_by(layer_name) %>%
  filter(year >= 1990) %>%
  summarize(year_min = min(year),
            year_max = max(year),
            targets = paste(unique(target) %>% sort(), collapse = ', ')) %>%
  ungroup() %>%
  arrange(layer_name) %>%
  mutate(layer_name = factor(layer_name, levels = rev(.$layer_name %>% unique), ordered = TRUE))

span_plot <- ggplot(year_spans, aes(x = layer_name, color = targets)) +
  ggtheme_plot(base_size = 8) +
  geom_linerange(aes(ymin = year_min, ymax = year_max), show.legend = FALSE) +
  labs(x = 'Layer name',
       y = 'Data year') +
  scale_color_manual(values = rep(brewer.pal(n = 8, name = 'Dark2'), 5)) +
  geom_text(aes(y = year_max, label = targets), color = 'grey20', size = 1.6, 
            vjust = 0, nudge_x = 0.1, hjust = 1, show.legend = FALSE) +
  coord_flip()

ggsave(file.path(dir_calc, 'explore/layers_data_years.png'), height = 6, width = 5)